<?php
class wxClassLib
{
	public $access_token;
	public $helper;
	public $pc;

	
	public function __construct($access_token)
	{
		include_once "wxHelper.php";
		//include_once "wxBizMsgCrypt.php";
		$this->access_token = $access_token;
		$this->helper = new wxHelper();
		//$this->pc = new wxBizMsgCrypt();
	}

	/*
	*获取用户标签
	*/
	public function get_usertag()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/tags/get?access_token=".$this->access_token;
		$res = json_decode($this->helper->httpGet($url), true);
		if(isset($res['tags']))
		{
			return $res;
		}else
		{
			return "";
		}
	}

	/*
	*创建用户标签
	*/
	public function create_usertag($name)
	{
		$url = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=".$this->access_token;
		$name = urlencode($name);
		$curlPost['tag']['name'] = $name;
		$curlPost = urldecode(json_encode($curlPost)); 
		$res = json_decode($this->helper->httpPost($curlPost, $url), true);
		if(isset($res['tag']))
		{
			return $res;
		}else
		{
			return "";
		}
	}

	/*
	*修改用户标签
	*/
	public function update_usertag($wid, $name)
	{
		$url = "https://api.weixin.qq.com/cgi-bin/tags/update?access_token=".$this->access_token;
		$name = urlencode($name);
		$curlPost['tag']['id'] = $wid;
		$curlPost['tag']['name'] = $name;
		$curlPost = urldecode(json_encode($curlPost));
		$res = json_decode($this->helper->httpPost($curlPost, $url), true);
		if($res['errcode'] == 0)
		{
			return true;
		}else
		{
			return false;
		}
	}

	/*
	*删除用户标签
	*/
	public function delete_usertag($wid)
	{
		$url = "https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=".$this->access_token;
		$curlPost['tag']['id'] = $wid;
		$curlPost = json_encode($curlPost);
		$res = json_decode($this->helper->httpPost($curlPost, $url), true);
		if($res['errcode'] == 0)
		{
			return true;
		}else
		{
			return false;
		}
	}

	/*
	*批量移动用户组
	*/
	public function move_users($openid_list,$tagid){
		$url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=".$this->access_token;
		if($openid_list != ""){
			$curlPost['openid_list'] = $openid_list;
			$curlPost['tagid'] = $tagid;
			$curlPost = json_encode($curlPost);
			$res = json_decode($this->helper->httpPost($curlPost, $url),true);
			if($res['errcode'] == '0'){
				return $res;
			}
			else{
				return "";
			}
		}
		else{
			return '';
		}
	}

	//取消标签
	public function cancel_tag($openid, $tag_id)
	{
		$url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token=".$this->access_token;
		$curlPost['openid_list'] = $openid;
		$curlPost['tagid'] = $tag_id;
		$curlPost = json_encode($curlPost);
		$res = json_decode($this->helper->httpPost($curlPost, $url),true);
		if($res['errcode'] == '0'){
				return $res;
			}
			else{
				return "";
			}
	}
	//获取用户标签
	public function get_user_tag($openid)
	{
		$url = "https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=".$this->access_token;
		$curlPost['openid'] = $openid;
		$curlPost = json_encode($curlPost);
		$res = json_decode($this->helper->httpPost($curlPost, $url),true);
		if(isset($res['tagid_list'])){
				return $res;
			}
			else{
				return "";
			}
	}

	/*
	*获取用户列表
	*/
	public function get_userlist()
	{
		//一次拉取调用最多拉取10000个关注者的OpenID
		//本实例目前只考虑拉取1000个
		$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$this->access_token;
		$res = json_decode($this->helper->httpGet($url), true);
		if($res['total']>0)
		{
			return $res;
		}else
		{
			return "";
		}
	}

	/*
	*获取用户信息
	*/
	public function get_userinfo($openid)
	{
		//一次拉取调用最多拉取10000个关注者的OpenID
		//本实例目前只考虑拉取1000个
		$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_CN ";
		$res = json_decode($this->helper->httpGet($url), true);
		if($res['openid']!="")
		{
			return $res;
		}else
		{
			return "";
		}
	}

	/*
	*批量获取用户信息，一次获取100ge
	*/
	public function get_userinfo_batchget($openidarr)
	{
		$url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=".$this->access_token;
		$curlPost['user_list'] = $openidarr;
		$curlPost = json_encode($curlPost);
		$res = json_decode($this->helper->httpPost($curlPost, $url), true);
		if(isset($res['user_info_list']))
		{
			return $res;
		}else
		{
			return "";
		}
	}

	/*
	*新增永久媒体文件
	*/
	public function up_media_towx($type, $img_path, $data)
	{
		$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$this->access_token."&type=".$type;
		/*$file_info=array(
		    'filename'=>'/images/1.png',  //国片相对于网站根目录的路径
		    'content-type'=>'image/png',  //文件类型
		    'filelength'=>'11011'         //图文大小
		);*/
        if($data['filename'] != "")
        {
        	$curlPost['media'] =  '@' . $img_path;
        	$curlPost['form-data'] = $data;
        	$res = json_decode($this->helper->httpPost($curlPost, $url), true);
        	if(isset($res['media_id']))
        	{
        		return $res;
        	}else
        	{
        		return "";
        	}
        }else
        {
        	return "";
        } 
	}

	/*
	*新增永久媒体文件
	*/
	public function up_video_towx($video_path, $data, $description)
	{
		$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$this->access_token."&type=video";
		/*$file_info=array(
		    'filename'=>'/images/1.png',  //国片相对于网站根目录的路径
		    'content-type'=>'image/png',  //文件类型
		    'filelength'=>'11011'         //图文大小
		);*/
        if($data['filename'] != "")
        {
        	$curlPost['media'] =  '@' . $video_path;
        	$curlPost['form-data'] = $data;

        	$title = urlencode($description['title']);
        	$introduction = urlencode($description['introduction']);
        	$description_new['title'] = $title;
        	$description_new['introduction'] = $introduction;
        	$curlPost['description'] = urldecode(json_encode($description_new));
			
        	$res = json_decode($this->helper->httpPost($curlPost, $url), true);
        	if(isset($res['media_id']))
        	{
        		return $res;
        	}else
        	{
        		return "";
        	}
        }else
        {
        	return "";
        } 
	}
	/*
	*新增临时文件
	*/
	public function up_temporary_media($type, $img_path, $data)
	{
		$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->access_token."&type=".$type;
		/*$file_info=array(
		    'filename'=>'/images/1.png',  //国片相对于网站根目录的路径
		    'content-type'=>'image/png',  //文件类型
		    'filelength'=>'11011'         //图文大小
		);*/
        if($data['filename'] != "")
        {
        	$curlPost['media'] =  '@' . $img_path;
        	$curlPost['form-data'] = $data;
        	$res = json_decode($this->helper->httpPost($curlPost, $url), true);
        	if(isset($res['media_id']))
        	{
        		return $res;
        	}else
        	{
        		return "";
        	}
        }else
        {
        	return "";
        } 
	}

	//新增永久图文素材
    public function up_news_towx($articles)
    {
    	$url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=".$this->access_token;
    	foreach ($articles as $key => $val) {
    		$articles[$key]['title'] = urlencode($val['title']);
    		$articles[$key]['author'] = urlencode($val['author']);
    		$articles[$key]['digest'] = urlencode($val['digest']);
    		$articles[$key]['content'] = urlencode($val['content']);
    	}
    	$curlPost['articles'] = $articles;
    	$curlPost = urldecode(json_encode($curlPost));
    	$res = json_decode($this->helper->httpPost($curlPost, $url), true);
    	if(isset($res['media_id']))
    	{
    		return $res;
    	}else
    	{
    		return "";
    	}
    }

	
	/*
	* 删除永久媒体文件 2016-5-16 jack
	* @param int $media_id 
	* return int 1/0
	*/
	public function del_media_towx($media_id)
	{

	    $url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=".$this->access_token;
	    $curlPost['media_id'] = $media_id;
	    $curlPost = json_encode($curlPost);
	    $res = json_decode($this->helper->httpPost($curlPost, $url), true);
	    if ($res['errcode'] == 0)
	    {
	        return true;
	    }
	    else
	    {
	        return false;
	    }
	}


	public function create_menu($menu)  
    {  
         //https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN  
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token;  
          
        $res = json_decode($this->helper->httpPost($menu, $url), true);
        if($res['errcode'] == 0)
        {
        	return true;
        }else
        {
        	return false;
        } 
    }  
  
    public function del_menu()  
    {  
        //https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN  
        $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$this->access_token;  
        $res = json_decode($this->helper->httpGet($url), true); 
        if($res['errcode'] == 0)
        {
        	return true;
        }else
        {
        	return false;
        }  
    }

    //给用户发送消息，只有48小时内向公众号发送过消息的用户才能接收到消息
    public function send_service($send_content, $openid)
    {
    	$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
    	$curlPost['touser'] = $openid;
    	$curlPost['msgtype'] = "text";
    	$curlPost['text']['content'] = urlencode($send_content);
    	$curlPost = urldecode(json_encode($curlPost));
    	
    	$res = json_decode($this->helper->httpPost($curlPost, $url), true);
        if($res['errcode'] == 0)
        {
        	return true;
        }else
        {
        	return false;
        } 
    }

    //群发消息
    public function send_mass_msg($type, $tag_id, $data)
    {
    	$url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=".$this->access_token;
    	$curlPost['filter']['is_to_all'] = false;
    	$curlPost['filter']['tag_id'] = $tag_id;
    	$curlPost['msgtype'] = $type;

    	if($type == 'text')
    	{
    		$curlPost['text']['content'] = urlencode($data['content']);
    	}elseif($type == 'image')
    	{
    		$curlPost['image']['media_id'] = $data['media_id'];
    	}
    	elseif($type == 'voice')
    	{
    		$curlPost['voice']['media_id'] = $data['media_id'];
    	}
    	elseif($type == 'video')
    	{
    		$curlPost['msgtype'] = "mpvideo"; 
    		$curlPost['mpvideo']['media_id'] = $data['media_id'];
    	}
    	elseif($type == 'news')
    	{
    		$curlPost['msgtype'] = "mpnews"; 
    		$curlPost['mpnews']['media_id'] = $data['media_id'];
    	}
    	$curlPost = urldecode(json_encode($curlPost));
    	
    	$res = json_decode($this->helper->httpPost($curlPost, $url), true);

    	if($res['errcode'] == 0)
    	{
    		return $res;
    	}else
    	{
    		return "";
    	}
    }
    //获取群发信息时的视频id
    public function up_mpvideo($data)
    {
    	$url = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=".$this->access_token;
    	$curlPost['media_id'] = $data['media_id'];
    	$curlPost['title'] = urlencode($data['title']);
    	$curlPost['description'] = urlencode($data['description']);

    	$curlPost = urldecode(json_encode($curlPost));
    	$res = json_decode($this->helper->httpPost($curlPost, $url), true);
    	if(isset($res['media_id']))
    	{
    		return $res;
    	}else
    	{
    		return "";
    	}
    }

    //获取群发信息时的图文信息id
    public function up_mpnews($articles)
    {
    	$url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=".$this->access_token;
    	foreach ($articles as $key => $val) {
    		$articles[$key]['title'] = urlencode($val['title']);
    		$articles[$key]['author'] = urlencode($val['author']);
    		$articles[$key]['digest'] = urlencode($val['digest']);
    		$articles[$key]['content'] = urlencode($val['content']);
    	}
    	$curlPost['articles'] = $articles;
    	$curlPost = urldecode(json_encode($curlPost));
    	$res = json_decode($this->helper->httpPost($curlPost, $url), true);
    	if(isset($res['media_id']))
    	{
    		return $res;
    	}else
    	{
    		return "";
    	}
    }

	/*
	新添加渠道二维码
	*/
	public function add_qrcode($expire_seconds,$action_name,$action_info){
		$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->access_token;
		if($action_info != ""){
			if($action_name =='QR_SCENE'){
				$curlPost['expire_seconds'] = $expire_seconds;
				$curlPost['action_name'] = $action_name;
				$curlPost['action_info'] = $action_info;
				$curlPost = json_encode($curlPost);
			}
			else{
				$curlPost['action_name'] = $action_name;
				$curlPost['action_info'] = $action_info;
				$curlPost = json_encode($curlPost);	
			}
			
			$res = json_decode($this->helper->httpPost($curlPost, $url), true);
			if($res['url']){
				return $res;
			}
			else{
				return false;
			}
		}
		
		
		
	}	

	/*
	 2016-5-21 jack 商家公众号编辑客服 [增加,删除,修改]
	 @param array $kf_list 客服的相关数据
	 @param string $type   编辑客服的类型[add_kf,upd_kf,del_kf]
	 */
    public function edit_kf($kf_list,$type)
    {
        //编辑客服url 增改删
        if ($type == 'add_kf')
            $url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=".$this->access_token;
        elseif ($type == 'upd_kf')
            $url = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=".$this->access_token;
        elseif ($type == 'del_kf')
            $url = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=".$this->access_token;
        
        //表单数据
        $curlPost['kf_account'] = $kf_list['pre_name'].'@'.$kf_list['wx_name'];
        $curlPost['nickname'] = urlencode($kf_list['kf_nick']);
        $curlPost['password'] = md5($kf_list['password']);
        if ($type == 'del_kf')
            $curlPost['password'] = $kf_list['password'];
        //调用接口

        $curlPost = urldecode(json_encode($curlPost));
        $res = json_decode($this->helper->httpPost($curlPost, $url),true);
        //返回状态
        if (!empty($res))
            return $res;
        else
            return false;
    }

    public function del_kf($kf_list,$type)
    {
    	$kf_account = $kf_list['pre_name'].'@'.$kf_list['wx_name'];
    	$url = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=".$this->access_token."&kf_account=".$kf_account;
    	$res = json_decode($this->helper->httpGet($url),true);
    	if (!empty($res))
            return $res;
        else
            return false;
    }

    public function send_custom_msg($type, $openid, $data)
    {
    	$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
    	$curlPost['touser'] = $openid;
     	if($type == "text")
    	{
    		$curlPost['msgtype'] = "text";
    		$content = urlencode($data['content']);
    		$curlPost['text']['content'] = $content;
    	}
    	$curlPost = urldecode(json_encode($curlPost));
        $res = json_decode($this->helper->httpPost($curlPost, $url),true);
        if($res['errcode'] == 0)
        {
        	return $res;
        }else
        {
        	return "";
        }
    }

	


}
?>